<?php
/**
 * BomRepo.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-13 23:13:14
 * @modified    2023-02-13 23:13:14
 *
 */

namespace App\Repositories\Product;

use App\Models\Product\Bom;
use App\Models\Product\Brand;
use App\Services\ImageServ;
use Carbon\Carbon;

class BomRepo
{
    public static function getList()
    {
        $perPage = (int)request('limit');
        if ($perPage < 1) {
            $perPage = 1;
        }
        $builder = Bom::query();

        $builder->with('brand');

        $name = trim(request()->get('name') ?? '');
        $specification = trim(request()->get('specification') ?? '');
        $model = trim(request()->get('model') ?? '');
        $uom = trim(request()->get('uom') ?? '');
        $brandId = intval(request()->get('brand_id') ?? '');

        if ($name) {
            $builder->where('name', 'like', "%{$name}%");
        }

        if ($specification) {
            $builder->where('specification', 'like', "%{$specification}%");
        }

        if ($model) {
            $builder->where('model', 'like', "%{$model}%");
        }

        if ($uom) {
            $builder->where('uom', 'like', "%{$uom}%");
        }

        if ($brandId) {
            $builder->where('brand_id', $brandId);
        }

        $result = $builder->paginate($perPage);
        $result->transform(function ($item) {
            return self::getDetail($item);
        });
        return $result;
    }

    public static function getDetail($bom)
    {
        $brand = $bom->brand;
        if (empty($brand)) {
            $bomBrand = [];
        } else {
            $bomBrand = [
                'id' => $brand->id,
                'name' => $brand->name,
                'image' => $brand->image,
                'image_url' => (new ImageServ($brand->image))->getUrl(),
                'sort_order' => $brand->sort_order,
                'status' => $brand->status,
                'create_time' => Carbon::parse($brand->create_time)->toDateTimeString(),
                'update_time' => Carbon::parse($brand->update_time)->toDateTimeString()
            ];
        }
        return [
            'id' => $bom->id,
            'name' => $bom->name,
            'specification' => $bom->specification,
            'model' => $bom->model,
            'uom' => $bom->uom,
            'create_time' => Carbon::parse($bom->create_time)->toDateTimeString(),
            'update_time' => Carbon::parse($bom->update_time)->toDateTimeString(),
            'brand' => $bomBrand
        ];
    }

    public static function fetch($id)
    {
        $bom = Bom::find($id);
        if (empty($bom)) {
            throw new \Exception('配件信息不存在');
        }
        $bom->with('brand');
        return self::getDetail($bom);
    }

    public static function store($id)
    {
        $bomData = self::requestData();

        if ($id) {
            $bom = Bom::find($id);
        } else {
            $bom = new Bom();
        }
        if (empty($bom)) {
            $bom = new Bom();
        }
        $bom->fill($bomData);
        $bom->saveOrFail();
        return self::fetch($bom->id);
    }

    public static function destroy($id)
    {
        $bom = Bom::find($id);
        if (empty($bom)) {
            throw new \Exception('配件信息不存在');
        }
        $productTypeBomCount = $bom->product_type_boms->count();
        if ($productTypeBomCount) {
            throw new \Exception("当前配件有{$productTypeBomCount}个品类正在使用，请先解绑再删除");
        }
        $bom->delete();
    }

    private static function requestData()
    {
        $requestData = request()->getContent();
        $requestArr = json_decode($requestData, true);

        $brandId = $requestArr['brand_id'] ?? 0;
        $brand = Brand::find($brandId);
        if (empty($brand)) {
            // throw new \Exception('品牌不存在');
        }

        return [
            'brand_id' => $brandId,
            'name' => $requestArr['name'] ?? '',
            'specification' => $requestArr['specification'] ?? '',
            'model' => $requestArr['model'] ?? '',
            'uom' => $requestArr['uom'] ?? '',
        ];
    }
}